// 引入接口文件
import { getInfo,verifyCodeFun,getCustomerTimeAxis} from './customerInfoServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  modelObj: {
    btnLoading: false,
  },
  loading: false,
  actionType: 'add',
  detail: {},
  timeList: []
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // 执行业务操作
          dispatch({type: 'queryInfo',payload: {query}}) 
          dispatch({type: 'getCustomerTimeAxis',payload: {query}}) 
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    // 查询记录
    *queryInfo({ payload }, { put, call, select }) {
      let {detail} = yield select(state => state[tmpModule.namespace])
      let params = {
          uniqueId:payload.query.id,
          status:payload.query.status
        }
        if(yield call(verifyCodeFun,{})){
        const { success,data } = yield call(getInfo, {...params})
        if(success){
          detail = data
          yield put({type: 'updateStore', payload: {detail}})
        }
      }
    },
    // 获取时间轴
    *getCustomerTimeAxis({ payload }, { put, call, select }) {
      let {timeList} = yield select(state => state[tmpModule.namespace])
      let params = {
          uniClientId: payload.query.id,
          status:payload.query.status
        }
        let { success, msg, data } = yield call(getCustomerTimeAxis, {...params})
        if(success){
          let list = []
          for(var item of data){
              for(var itemed of item.axisDetail){
                list.push(itemed)
              }
          }
          timeList = list
          yield put({type: 'updateStore', payload: {timeList}})
        }
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
